﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace SnippetBox.businesslogic
{
  class XMLSerializer
  {
    public static void SerializeToXML(List<Snippet> snippets)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(List<Snippet>));
      TextWriter textWriter = new StreamWriter(@"C:\SnippetBox_Snippets.xml");
      serializer.Serialize(textWriter, snippets);
      textWriter.Close();
    }

    public static void SerializeToXML(List<Topic> topics)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(List<Topic>));
      TextWriter textWriter = new StreamWriter(@"C:\SnippetBox_Topics.xml");
      serializer.Serialize(textWriter, topics);
      textWriter.Close();
    }

    public static List<Snippet> DeserializeFromXML(List<Snippet> sl)
    {
      XmlSerializer deserializer = new XmlSerializer(typeof(List<Snippet>));
      TextReader textReader = new StreamReader(@"C:\SnippetBox_Snippets.xml");
      List<Snippet> snippets;
      snippets = (List<Snippet>)deserializer.Deserialize(textReader);
      textReader.Close();

      return snippets;
    }

    public static List<Topic> DeserializeFromXML(List<Topic> tl)
    {
      XmlSerializer deserializer = new XmlSerializer(typeof(List<Topic>));
      TextReader textReader = new StreamReader(@"C:\SnippetBox_Topics.xml");
      List<Topic> topics;
      topics = (List<Topic>)deserializer.Deserialize(textReader);
      textReader.Close();

      return topics;
    }
  }
}
